package com.itheima.controller;

import com.itheima.domain.Book;
import com.itheima.domain.BookSelective;
import com.itheima.domain.User;
import com.itheima.service.BookService;
import com.itheima.util.RequestSessionUtil;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;


/**
 * Created with IntelliJ IDEA.
 * @Author: auuuu4
 * @Date: 2024/07/09/15:15
 * @Description:
 */
@RestController
@RequestMapping(value = "/book")
public class BookController {
    @Autowired
    BookService bookService;
    @GetMapping("/selectNewbooks")
    public PageResult selectNewBooks(HttpServletRequest request, HttpServletResponse response){
        try {
            PageResult pageResult = bookService.selectNewBooks(1,5);
            RequestSessionUtil.setAttribute(request,"pageResult",pageResult);
            response.sendRedirect("/cloudlibrary/admin/books_new.jsp");
            return pageResult;
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(0,null);

        }
    }
    @GetMapping("/findById")
    public Result findById(@RequestParam int id){
        return Result.success(bookService.findById(id));
    }


    @PostMapping("/borrowBook")
    public Result borrowBook(@RequestParam Integer id, @RequestParam String name, @RequestParam String isbn, @RequestParam String press
            , @RequestParam String author, @RequestParam Integer pagination, @RequestParam Date returnTime, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if(bookService.borrowBook(new Book(id,name,isbn,press,author,pagination,null,null,"1",RequestSessionUtil.getUserInfo(request).getName(),new Date(System.currentTimeMillis()),returnTime)) > 0){
//            request.getRequestDispatcher("/cloudlibrary/admin/search").forward(request,response);
            return Result.success();
        }

        else
            return Result.fail();
    }

    @GetMapping("/returnBook")
    public Result returnBook(@RequestParam Integer id,HttpServletRequest request,HttpServletResponse response){
        if(bookService.returnBook(id) > 0)
            return Result.success();
        else
            return Result.fail();
    }

    @GetMapping("/returnConfirm")
    public Result returnConfirm(@RequestParam Integer id,HttpServletRequest request,HttpServletResponse response){
        if(bookService.returnConfirm(id) > 0)
            return Result.success();
        else
            return Result.fail();
    }
    @PostMapping("/addBook")
    public Result addBook(@RequestParam String name,@RequestParam String isbn,@RequestParam String press
            ,@RequestParam String author,@RequestParam Integer pagination, @RequestParam Double price,@RequestParam String status){

        if(bookService.addBook(new Book(null,name,isbn,press,author,pagination,price,null,status,null,null,null)) > 0)
            return Result.success();
        else
            return Result.fail();
    }

    @RequestMapping("/search")
    public void search(String name,String author,String press,Integer pageNum ,Integer pageSize,HttpServletRequest request,HttpServletResponse response){
        try {
            BookSelective selective = new BookSelective(name,author,press,null);
            RequestSessionUtil.setAttribute(request,"pageResult",bookService.selectBorrowedBooksSelective(pageNum,pageSize, selective));
            request.getRequestDispatcher("/admin/books.jsp").forward(request,response);

        }catch (Exception e){
            e.printStackTrace();

        }
    }

    @RequestMapping("/searchBorrowed")
    public void searchBorrowed(String name,String author,String press,Integer pageNum ,Integer pageSize,HttpServletRequest request,HttpServletResponse response){
        try {
            User user = RequestSessionUtil.getUserInfo(request);
            BookSelective selective = new BookSelective(name,author,press,user.getName());
            if(user.isAdmin())
                selective.setUname(null);
            RequestSessionUtil.setAttribute(request,"pageResult",bookService.selectBorrowedBooksSelective(pageNum,pageSize, selective));
            request.getRequestDispatcher("/admin/book_borrowed.jsp").forward(request,response);
        }catch (Exception e){
            e.printStackTrace();

        }

    }
    @PostMapping("/editBook")
    public Result editBook(@RequestParam String name,@RequestParam String isbn,@RequestParam String press
            ,@RequestParam String author,@RequestParam Integer pagination, @RequestParam Double price,@RequestParam String status){

        if(bookService.updateBook(new Book(null,name,isbn,press,author,pagination,price,null,status,null,null,null)) > 0)
            return Result.success();
        else
            return Result.fail();
    }

}
